import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/3/2
 * Time:20:20
 **/

public enum TestEnum {
    RED("red",1),GREEN("green",2),BLACK("black",3);

    public String color;
    public int ordinal;
    //默认是私有的构造方法
    TestEnum(String color,int ordinal) {
        this.color = color;
        this.ordinal = ordinal;
    }

    public static void reflectPrivateConstructor() {
        try {
            Class<?> cl = Class.forName("TestEnum");

            Constructor<?> constructor =
                    cl.getDeclaredConstructor(String.class,int.class,String.class,int.class);

            constructor.setAccessible(true);

            TestEnum yellow = (TestEnum)constructor.newInstance("黄色", 4,"父类的参数1",110);

            System.out.println(yellow);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }

    public static void main2(String[] args) {
        //将当前的枚举对象，封装为数组
        TestEnum[] testEnums = TestEnum.values();
       /* for (TestEnum testEnum :testEnums) {
            System.out.println(testEnum);
        }*/
        for (int i = 0; i < testEnums.length; i++) {
            System.out.println(testEnums[i].ordinal());
        }
        //根据字符串解析出来一个枚举对象
        System.out.println(TestEnum.valueOf("GREEN"));

        System.out.println(RED.compareTo(BLACK));
    }
    public static void main1(String[] args) {
        TestEnum testEnum = TestEnum.BLACK;
        switch (testEnum) {
            case RED:
                System.out.println("red");
                break;
            case BLACK:
                System.out.println("black");
                break;
            case GREEN:
                System.out.println("black");
                break;
            default:
                break;
        }
    }
}



